package com.hsc.www;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication
@Controller
@Configuration
public class Db8073 {
    public static void main(String[] args) {
        SpringApplication.run(Db8073.class, args);
    }

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    @Bean
    public  JdbcTemplate createJdbcTemplate( DataSource dataSource){
       return new JdbcTemplate(dataSource);
    }


    @ResponseBody
    @RequestMapping(value = "dbUsers")
    public List<Map> dbUsers() {
        //user表是mysql库中的基础表
        return jdbcTemplate.query("select User, Password  from user", (resultSet,i)-> {
            Map user = new HashMap();
            user.put("user", resultSet.getString(1));
            user.put("password", resultSet.getString(2));
            return user;
        });
    }





}